package com.vess.ratingweb;

import java.io.InputStream;
import java.util.Calendar;
import java.util.concurrent.ExecutionException;
import com.vess.ratingweb.model.JDBCAdapter;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("NewApi")
public class Information extends Activity {

	private Button btnSubmit;
	private CheckBox mCkb;
	private String mStrTks, mStrLogo;
	private EditText mEdtUser, mEdtEmail, mEdtReview;
	private JDBCAdapter mJDBC;
	private int mIntNoLogin;
	private TextView mTxtTks;
	private String mStrUsername, mStrCoupon;
	private int mIntEvaluate;
	private int mIntUserId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.information);
		mIntNoLogin = getIntent().getIntExtra("nologin", 0);
		mIntEvaluate = getIntent().getIntExtra("evaluate", 0);
		mStrLogo = getIntent().getStringExtra("logo");
		mStrTks = getIntent().getStringExtra("thankyou");
		mStrUsername = getIntent().getStringExtra("username");
		mStrCoupon = getIntent().getStringExtra("coupon");
		mIntUserId = getIntent().getIntExtra("id", 0);

		mEdtUser = (EditText) findViewById(R.id.ediName);
		mEdtEmail = (EditText) findViewById(R.id.ediMail);
		mEdtReview = (EditText) findViewById(R.id.ediReview);
		mTxtTks = (TextView) findViewById(R.id.txtInforTks);
		mCkb = (CheckBox) findViewById(R.id.chbAgreement);

		mTxtTks.setText(mStrTks);

		btnSubmit = (Button) findViewById(R.id.btnSubmit);

		mJDBC = new JDBCAdapter(Information.this);

		new DownloadImageTask((ImageView) findViewById(R.id.imgInforLogo))
				.execute("http://reputation.bonothemes.com/user/php/uploads/"
						+ mStrLogo);

		mEdtUser.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				EditText edt = (EditText) v;
				edt.setText("");
				return false;
			}
		});

		mEdtReview.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				EditText edt = (EditText) v;
				edt.setText("");
				return false;
			}
		});

		mEdtEmail.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				EditText edt = (EditText) v;
				edt.setText("");
				return false;
			}
		});

		btnSubmit.setOnClickListener(new OnClickListener() {

			@SuppressWarnings("deprecation")
			@Override
			public void onClick(View v) {
				if (mCkb.isChecked()) {
					String[] strInfo = new String[4];
					strInfo[0] = mEdtUser.getText().toString();
					strInfo[1] = mEdtReview.getText().toString();
					strInfo[2] = mEdtEmail.getText().toString();
					strInfo[3] = String.valueOf(Calendar.getInstance()
							.getTimeInMillis());
					if ((strInfo[0].equals("") == true)
							|| (strInfo[1].equals("") == true)
							|| (strInfo[2].equals("") == true)) {
						Toast.makeText(Information.this,
								"Please fill the blank", Toast.LENGTH_LONG)
								.show();
					} else {
						Log.i("insert", "ok");
						int sdk = android.os.Build.VERSION.SDK_INT;
			            if(sdk < android.os.Build.VERSION_CODES.HONEYCOMB) {
			                android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
			                clipboard.setText(mEdtReview.getText().toString());
			            } else {
			                android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); 
			                android.content.ClipData clip = android.content.ClipData.newPlainText("user review",mEdtReview.getText().toString());
			                clipboard.setPrimaryClip(clip);
			            }
						insertReview(strInfo);
						if (mIntNoLogin == 0) {
							Intent intent = new Intent(Information.this,
									Login.class);
							intent.putExtra("username", mStrUsername);
							intent.putExtra("timestamp", strInfo[3]);
							intent.putExtra("logo", mStrLogo);
							intent.putExtra("coupon", mStrCoupon);
							intent.putExtra("id", mIntUserId);
							startActivity(intent);
						} else {
							Intent intent = new Intent(Information.this,
									Result.class);
							intent.putExtra("timestamp", strInfo[3]);
							intent.putExtra("logo", mStrLogo);
							intent.putExtra("coupon", mStrCoupon);
							intent.putExtra("id", mIntUserId);
							startActivity(intent);
						}
					}
				} else {
					Toast.makeText(Information.this, "Please check Agreement!",
							Toast.LENGTH_SHORT).show();
				}
			}
		});
	}

	protected void insertReview(String[] strInfo) {
		InsertSitesReviewsDB lp = new InsertSitesReviewsDB();
		lp.execute(strInfo);
		String strResult = "";
		try {
			strResult = lp.get();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		if (strResult.equals("-1") == true) {
			Toast.makeText(Information.this, "Can't not to connect to Server!",
					Toast.LENGTH_LONG).show();
		}
	}

	private class InsertSitesReviewsDB extends
			AsyncTask<String, String, String> {

		private String strResult;

		@Override
		protected String doInBackground(String... params) {
			strResult = mJDBC
					.insertSitesReviewsDB(params[0], "Lower Rating", params[1],
							params[2], params[3], mIntEvaluate, mStrUsername);
			Log.i("doInBG", strResult);
			return strResult;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);

		}
	}

	private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
		ImageView bmImage;

		public DownloadImageTask(ImageView bmImage) {
			this.bmImage = bmImage;
		}

		protected Bitmap doInBackground(String... urls) {
			String urldisplay = urls[0];
			Bitmap mIcon11 = null;
			try {
				InputStream in = new java.net.URL(urldisplay).openStream();
				mIcon11 = BitmapFactory.decodeStream(in);
			} catch (Exception e) {
				Log.e("Error", e.getMessage());
				e.printStackTrace();
			}
			return mIcon11;
		}

		protected void onPostExecute(Bitmap result) {
			bmImage.setImageBitmap(result);
		}
	}
}
